package cn.tedu.knows.auth;


import org.apache.catalina.filters.CorsFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class KnowsAuthApplication {

    public static void main(String[] args) {
        SpringApplication.run(KnowsAuthApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    // 注册过滤器使其在项目中生效的配置
    @Bean
    public FilterRegistrationBean registrationBean(){
        FilterRegistrationBean<CorsFilter> bean=
                new FilterRegistrationBean<>();
        // 设置过滤器生效路径(全部路径生效)
        bean.addUrlPatterns("/*");
        // 设置过滤器的优先级(多个过滤器运行的顺序,优先级高先运行)
        // 这里设置它为最高优先级,意思就是最先运行
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        // 设置过滤器对象
        bean.setFilter(new CorsFilter());
        return bean;
    }
}
